const Mixins = {}
let wraper = require('../component/wraper.vue')
let head = require('../component/head.vue')
let button = require('../component/button.vue')
let page = require('../component/page.vue')
let MyDiv = require('../component/my_div.vue')
let MyText = require('../component/my_text.vue')
const nav = weex.requireModule('navigator')
const zh = require('../lang/zh.js')
const pref = weex.requireModule("pref")
const net = require('../utils/http.js')
const ac = pref.get('account') || {}
const current_user_key = pref.getString('current_user_key')

// Vue.prototype.$net = net
Mixins.install = (Vue, options) => {
  Vue.mixin({
    components: {
      wraper,
      head,
      button,
      page,
      MyDiv,
      MyText
    },
    data(){
      return {
        modal: {},
        navigator: {},
        loading: false,
        locale: 'zh',
        lang:zh,
        theme:pref.getString('theme') || 'day'
      }
    },
    watch: {
      locale(val) {
        pref.setString('locale', val)
        this.lang = require(`../lang/${val}.js`)
        this.$forceUpdate()
      },
      theme(val) {
        pref.setString('theme', val)
        var page = weex.requireModule("page")
        page.reload()
      }
    },
    computed: {
      current_user(){
        if(Object.keys(ac).length>0){
          const curr_ac = ac[current_user_key]
          return curr_ac && curr_ac.user
        } else {
          return {}
        }
      },
      current_wallet() {
        if (Object.keys(ac).length > 0) {
           const curr_ac = ac[current_user_key]
          return curr_ac && curr_ac.wallet
        } else {
          return {}
        }
      }
    },
    methods: {
      toast(msg)
      {
        if (this.modal && this.modal.toast)
          this.modal.toast({message: msg})
      },
      alert(msg)
      {
        this.modal.alert({message: msg})
      },
      push(url, param)
      {
        if (!param)
          this.navigator.push(url)
        else
          this.navigator.pushParam(url, param)
      },
      getScreenHeight() {
        return 750 / weex.config.env.deviceWidth * weex.config.env.deviceHeight
      },
      $t(key) {
        if (!key) return ''
        const keys = key.split('.')
        return this.lang[keys[0]] && this.lang[keys[0]][keys[1]] || key
      },
    },
    created()
    {
      this.navigator = nav
      this.modal = weex.requireModule('modal')
      this.locale = pref.getString('locale') || 'zh'
      this.theme = pref.getString('theme') || 'day'
      this.$http = net
      this.pref = pref
      var globalEvent = weex.requireModule('globalEvent');
      globalEvent.addEventListener("onPageInit", (param) => {
        if (WXEnvironment.platform === 'android') {
          let keyboard = weex.requireModule('keyboard')
          keyboard.setKeyboardMode('adjust_pan')
        }
       
       
        let p = param
        if (param && p.param && p.bubbles)
          p = p.param
        if (this.onLoad != undefined)
          this.onLoad(p)
      });

    }

  })
}
const install = Mixins.install
export {install}
export  default Mixins